package com.lm.upms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lm.common.core.util.R;
import com.lm.common.log.annotation.SysLog;
import com.lm.common.log.enums.BusinessType;
import com.lm.common.security.util.SecurityUtils;
import com.lm.upms.api.dto.UserDTO;
import com.lm.upms.api.entity.SysUser;
import com.lm.upms.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 用户信息
 *
 * @author lm
 * @date 2019/7/2 10:42
 */
@Api(value = "用户信息接口服务", description = "用户信息接口服务")
@Slf4j
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService userService;

    /**
     * 获取当前登录的用户信息
     *
     * @return
     */
    @ApiOperation(value = "获取当前登录的用户信息", notes = "获取当前登录的用户信息")
    @GetMapping(value = {"/info"})
    public R info() {
        log.info("~~~~~~~~~~~~~~~~~info()~~~~~~~~~~~~~~~~~~~~~~~1: {}, 2:{}", SecurityUtils.getUser(), SecurityUtils.getRoles());
        String username = SecurityUtils.getUser().getUsername();
        SysUser user = userService.findByUsername(username);
        if (user == null) {
            return R.error("获取当前用户信息失败！");
        }
        return R.success(userService.findUserInfo(user));
    }

    /**
     * 根据用户名查用户全部信息
     *
     * @return
     */
    @GetMapping("/info/{username}")
    public R info(@PathVariable String username) {
        SysUser user = userService.findByUsername(username);
        if (user == null) {
            return R.error(String.format("用户信息为空, %s", username));
        }
        return R.success(userService.findUserInfo(user));
    }

    /**
     * 分页查询用户
     *
     * @param page    参数集
     * @param userDTO 查询参数列表
     * @return
     */
    @GetMapping("/page")
    public R getUserPage(Page page, UserDTO userDTO, @RequestParam Map<String, Object> params) {
        return R.success(userService.getUsersWithRolePage(page, userDTO));
    }

    /**
     * 通过ID查询用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R user(@PathVariable Integer id) {
        return R.success(userService.selectUserVoById(id));
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param username 用户名
     * @return
     */
    @GetMapping("/details/{username}")
    public R user(@PathVariable String username) {
        SysUser cond = new SysUser();
        cond.setUsername(username);
        return R.success(userService.getOne(new QueryWrapper<>(cond)));
    }

    /**
     * 添加用户
     *
     * @param userDto 用户信息
     * @return
     */
    @SysLog(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("save")
    public R saveUser(@RequestBody UserDTO userDto) {
        return R.success(userService.saveUser(userDto));
    }

    /**
     * 更新用户信息
     *
     * @param userDto 用户信息
     * @return
     */
    @SysLog(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("update")
    public R updateUser(@Valid @RequestBody UserDTO userDto) {
        return R.success(userService.updateUser(userDto));
    }

    /**
     * 修改个人信息, 未测试
     *
     * @param userDto userDto
     * @return
     */
    // @SysLog("修改个人信息")
    // @SysLog(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/editInfo")
    public R updateUserInfo(@Valid @RequestBody UserDTO userDto) {
        return userService.updateUserInfo(userDto);
    }

    /**
     * 获取上级部门用户列表
     *
     * @param username 用户名称
     * @return
     */
    @GetMapping("/ancestor/{username}")
    public R listAncestorUsers(@PathVariable String username) {
        return R.success(userService.listAncestorUsers(username));
    }

    /**
     * 删除用户信息
     *
     * @param ids
     * @return
     */
    @SysLog(title = "用户管理", businessType = BusinessType.DELETE)
    @PostMapping("remove")
    public R remove(String ids) throws Exception {
        // SysUser sysUser = userService.getById(ids);
        return R.success(userService.deleteUserById(ids));
    }

//    /**
//     * 删除用户
//     * @throws Exception
//     */
//    @SysLog(title = "用户管理", businessType = BusinessType.DELETE)
//    @PostMapping("remove")
//    public R remove(String ids) throws Exception
//    {
//        return toAjax(sysUserService.deleteUserByIds(ids));
//    }

}